package service.impl;

import dao.MemberMapper;
import entity.Member;
import exception.DuplicateUsernameException;
import exception.UserNotFoundException;
import framework.bean.factory.ObjectFactory;
import framework.bean.factory.annotation.Component;
import service.MemberService;

import java.util.Date;

@Component("memberServiceTarget")
public class MemberServiceImpl implements MemberService {
    @Override
    public void regist(Member member) throws DuplicateUsernameException {
        MemberMapper memberMapper= (MemberMapper) ObjectFactory.getObject("memberMapper");


        Member m=memberMapper.selectByUsername(member.getUsername());

        if(m!=null){
            throw new DuplicateUsernameException("用户名已存在");
        }

        member.setRegtime(new Date());

        memberMapper.insert(member);


    }

    @Override
    public Member login(String username, String password) throws UserNotFoundException {
        MemberMapper memberMapper= (MemberMapper) ObjectFactory.getObject("memberMapper");


        Member member=memberMapper.selectByUsernameAndPassword(username,password);

        if(member==null){
            throw new UserNotFoundException("用户名或密码错误");
        }

        return member;
    }
}
